<!--
 * @Author: LAB
 * @Date: 2021-01-27 23:13:35
 * @Description: 上传图片
 * @FilePath: \file-tools\upload-img\upload-img.vue
-->
<template>
  <div class="upload-img-box">
    <upload-img-base
      class="img-item"
      @getImg="getImg"
      :maxLength="addLength"
      @fileTools="fileTools"
    ></upload-img-base>
    <div class="img-item" v-for="(img, index) in imgList" :key="index">
      <el-image
        style="width: 100%; height: 100%"
        :src="img"
        alt=""
        :preview-src-list="imgList"
      />
      <div class="remove-img" @click="removeImg(index)">删除</div>
    </div>
  </div>
</template>

<script>
import uploadImgBase from "./upload-img-base"
export default {
  props: {
    // 上传图片数量
    maxLength: {
      type: Number,
      default: 10
    },
    // 原始图片
    imgList: {
      type: Array,
      default: function () {
        return []
      }
    },
    // 初始化后的uploadTools
    fileTools: {
      type: Object,
      default: function () {
        return null
      }
    }
  },
  computed: {
    addLength() {
      return this.maxLength - this.imgList.length
    }
  },
  components: {
    uploadImgBase
  },

  methods: {
    // 获取上传图片
    getImg(imgList) {
      this.$emit("getImg", imgList)
    },
    // 删除图片
    removeImg(index) {
      let imgs = this.imgList.splice(index, 1)
      this.$emit("getImg", imgs)
    }
  }
}
</script>

<style lang="scss" scoped>
$width: 120px;
$height: 120px;
$margin: 12px;
.upload-img-box {
  display: flex;
  flex-wrap: wrap;
  .img-item {
    width: $width;
    height: $height;
    margin: $margin;
    background-color: #fbfdff;
    border: 1px dashed #c0ccda;
    border-radius: 6px;
    box-sizing: border-box;
    position: relative;
    overflow: hidden;
    .remove-img {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      top: 100%;
      opacity: 0.7;
      transition: all 0.3s;
      text-align: center;
      font-size: 16px;
      color: #fff;
      background-color: #333;
    }
    &:hover {
      .remove-img {
        top: 60%;
        padding: 10%;
      }
    }
  }
}
</style>